package cn.cdeden.knowledge.controller.online;


import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.Jurisdiction;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.JurisdictionFactory;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.JurisdictionResponse;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.domain.dto.OnlinePreviewInfoDto;
import cn.cdeden.knowledge.domain.vo.FilesVersionsVo;
import cn.cdeden.knowledge.enums.FileEnums;
import cn.cdeden.knowledge.enums.ResultEnum;
import cn.cdeden.knowledge.service.FilesRecentlyViewedService;
import cn.cdeden.knowledge.service.FilesVersionsService;
import cn.cdeden.system.api.model.LoginUser;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 在线文件:处理模块
 */
@Controller
@Slf4j
//@Api(tags = "在线文件:处理模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/online")
public class OnlinePreviewController {

    private final JurisdictionFactory factory;


    private final FilesVersionsService versionsService;




    /**
     * 文件历史版本查看
     * @param info
     * @return
     */
//    @Log("文件历史版本查看")
//    @ApiOperation("文件历史版本查看")
    @ResponseBody
    @PostMapping(value = "/selectFilesVersions")
    public R selectFilesVersions(@RequestBody OnlinePreviewInfoDto info) throws BadRequestException {
        String type = info.getType();
        Jurisdiction jurisdiction = factory.getJurisdiction(type);
        if (ObjectUtils.isEmpty(jurisdiction)) {
            throw new BadRequestException("错误请求类型");
        }
        JurisdictionResponse<FilesTree> preview = jurisdiction.getFileDate(info.getId(),info.getInfo(), String.valueOf(LoginHelper.getUserId()));
//        JurisdictionResponse<FilesTree> preview = jurisdiction.getFileDate(info.getId(),info.getInfo(), String.valueOf(FileUserUtils.getUser().getId()));
        if (ObjectUtils.isNotEmpty(preview) && preview.getState() && ObjectUtils.isNotEmpty(preview.getData())) {
            FilesTree data = preview.getData();
            List<FilesVersionsVo> versionsVos = versionsService.getVersionsList(data.getStorageType(), String.valueOf(data.getStorageId()), type);
            versionsVos.forEach(vo -> {
                String file = vo.getSourceFile();
                vo.setLibraryType(file);
                vo.setSourceFile(null);
                vo.setCreateNickName(vo.getCreateId());
            });
            return R.ok(versionsVos);
//            return BaseRespResult.successResult(versionsVos);
        }
//        return BaseRespResult.errorResult("200", "400", preview.getMessage());
        return R.fail(ResultEnum.PERM_ERROR.getNum());
//        return BaseRespResult.errorResult(ResultEnum.PERM_ERROR.getNum(),preview.getMessage());
    }
}
